<?php

namespace App\Admin\Forms;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

class AuditForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public object $model;

    public function handle(array $input)
    {
        $id = $this->payload['id'];

        $table_name = $this->payload['table'];

        try {
            DB::table($table_name) -> where('id',$id) -> update($input);
            return $this->response()->success('操作成功')->refresh();
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return $this->response()->error('表'.$table_name.'不存在');
        } catch (\Illuminate\Database\QueryException $e) {
            return $this->response()->error('审核数据不存在');
        } catch (\Exception $e) {
            return $this->response()->error($e->getMessage());
        }
    }

    public function form()
    {
        $this->radio('audit_status', '审核状态') -> options([1 => '审核成功', 2 => '审核驳回']) ->default(1) -> required();

        $this->textarea('audit_response', '审核结果') -> required();
    }

}
